// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import '../theme/index.css'
import 'lib-flexible/flexible.js'
import ElementUI from 'element-ui';
import './assets/iconfont/iconfont.css'
import 'font-awesome/css/font-awesome.css'
import 'iview/dist/styles/iview.css'
import 'element-ui/lib/theme-chalk/index.css';
import i18n from './i18n/i18n'
import store from './vuex'
import NProgress from 'nprogress'
import { getToken } from '@/utils/auth' 
import { filterAsyncRoutes,hasPermission,GetAllRouter } from '@/utils/asyncRouter.js'
import '@/common/flexible.js';
import Icon from 'vue-awesome/components/Icon';
import VueRouter from 'vue-router'
import JsonViewer from 'vue-json-viewer'

Vue.use(JsonViewer)
Vue.use(VueRouter)

NProgress.configure({ showSpinner: true }) // 进度条配置
Vue.use(ElementUI)
Vue.component('icon', Icon);
Vue.config.productionTip = false



 
Vue.filter('dateFormat', function(originVal) {
 // const dt = new Date(originVal*1000) 毫秒时间戳 可自行判断
    const dt = new Date(originVal* 1000)
    const y = dt.getFullYear()
    const m = (dt.getMonth() + 1 + '').padStart(2, '0')
    const d = (dt.getDate() + '').padStart(2, '0')
 
    const hh = (dt.getHours() + '').padStart(2, '0')
    const mm = (dt.getMinutes() + '').padStart(2, '0')
    const ss = (dt.getSeconds() + '').padStart(2, '0')
    return `${y}年${m}月${d}日 ${hh}时:${mm}分:${ss}秒`
})




const whiteList = ['/login'] 

const originalPush = VueRouter.prototype.push
   VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

function CheckRouter(to,all) {
    var m = to.matched
    var path = to.path
    if (to.redirectedFrom != undefined) {
        for (var i = 0; i < m.length; i++) {
            if (m[i].path === to.redirectedFrom){
                return true
            }
        }
        for ( var i = 0;i < all.length; i++){
            if (all[i].children != undefined ){
                for (var j = all[i].children.length - 1; j >= 0; j--) {
                    if (all[i].children[j].path === to.redirectedFrom){
                        return true
                    }
                }
            }
            if (all[i].path === to.redirectedFrom){
                return true
            }
        }
        return false
    } else{
        for ( var i = 0;i < all.length; i++){
            if (all[i].children != undefined ){
                for (var j = all[i].children.length - 1; j >= 0; j--) {
                    if (all[i].children[j].path === to.path){
                        return true
                    }
                }
            }
            if (all[i].path === to.path){
                return true
            }
        }
    }
    return false
}

function permissionGo(to, from, next) {
    var u = sessionStorage.userId;
    const hasToken = getToken()
    var that =this;
    if (hasToken) {
        if (to.path === '/login') {
            next({path: '/index'}, () => {})
        }else if (to.redirectedFrom != undefined){
            if (to.redirectedFrom == "/" ||to.redirectedFrom == "/index"){
                next({path:"/index"})
                return 1
            }
            if (store.getters.router == undefined){
                !async function getAddRouters () {
                    await store.dispatch('getInfo', store.getters.token)
                    await store.dispatch('newRoutes', store.getters.info.role)
                    await router.addRoutes(store.getters.addRouters)
                    sessionStorage.setItem("routers",JSON.stringify(store.getters.routers))
                }()
            }
            if (!CheckRouter(to,store.getters.allRouters)&&!CheckRouter(to,store.getters.routers)){
                console.log("404");
                next({path:"/404"})
                return 2
            }else if (!CheckRouter(to,store.getters.routers)){
                console.log("403");
                next({path:"/403"})
                return 3
            }else{
              console.log("刷新");
              next({path:to.redirectedFrom})
              // return 4
            }
        }else  {
            if (!store.getters.info.role) {
                // const role = ['/markdown', '/erji', '/erji2', '/siji', '/wuji']
                !async function getAddRouters () {
                    await store.dispatch('getInfo', store.getters.token)
                    await store.dispatch('newRoutes', store.getters.info.role)
                    await router.addRoutes(store.getters.addRouters)
                    sessionStorage.setItem("routers",JSON.stringify(store.getters.routers))
                    next({path: to.path}, () => {})
                }()
                if (to.path != "/"){
                    next({path: to.path}, () => {})
                }else{
                    next({path:"/"})
                }
            }else {
                if (to.redirectedFrom != undefined ){
                  if (CheckRouter(to,store.getters.allRouters) ){
                    console.log("没有权限");
                    next({path:"/403"})
                  } else {
                    console.log("找不到页面");
                    next({path:"/404"})
                  }
                }else if (!CheckRouter(to,store.getters.routers)){
                    console.log("没有权限");
                    next({path:"/403"})
                }
            }
        }
        
    } else if (to.path != '/login') {
        sessionStorage.clear();
        next({path: `/login?redirect=${to.path}`})
    } 
    next()

    
    return 7
}

router.beforeEach((to, from, next) => {
    permissionGo(to, from, next) 
})

router.afterEach(() => {
    // 结束精度条
    NProgress.done()
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  i18n,
  components: { App },
  template: '<App/>'
})
